
const Wcore = (function(){
	
	var result = {
		init:function(){

			this.token = "";
		},
		import:function(url, callback) {
			var script = document.createElement("script");
			script.type = "text/javascript";
			if(typeof(callback) != "undefined"){
				if (script.readyState) {
				  script.onreadystatechange = function () {
					if (script.readyState == "loaded" || script.readyState == "complete") {
					  script.onreadystatechange = null;
					  callback();
					}
				  };
				} else {
				  script.onload = function () {
					callback();
				  };
				}
			}

			script.src = theme_path+url;
			
			
			if(document.body){
				document.body.appendChild(script);
			}
			else{
				document.head.appendChild(script);
			}
			
		},
		//保存数据
		setData:function(key,data){

			var data = data!==false?data:'';
			
			if(!window.localStorage){
					console.log("浏览器不支持localstorage");
					return false;
				}else{
					var storage = window.localStorage;
					
					if(key==''){
						storage[key] = null;
					}
					if(typeof(data)=='object'){
						storage[key] = 'json|'+JSON.stringify(data);
					}
					else{
						storage[key] = data;
					}
					
					return true;
				}
			return false;
		},
		//获取数据
		getData:function(key){
			var key = key ||'';

			if(!window.localStorage){
				console.log("浏览器不支持localstorage");
				return false;
			}else{
				var storage = window.localStorage;
				if(key==''){
					return storage;
				}
				var str = storage.getItem(key);
				if(!str){
					return false;
				}
				
				if(str.substr(0,5)=='json|'){
					str = str.replace(/^json\|/i,'');
					
					try{
						let data = eval('(' +str+ ')');
						return data;
					}
					catch(e){
						console.log(e);
					}
				}
				else{
					return str;
				}
			}
			return false;
		},
		getCache_size:function(){
			
			if(!window.localStorage) {
				console.log('浏览器不支持localStorage');
			}
			var size = 0;
			for(let item in window.localStorage) {
				if(window.localStorage.hasOwnProperty(item)) {
					var str = window.localStorage.getItem(item);
					
					size += lenFor(str);
				}
			}
			console.log(size);
			return size;
		},
		prefix:function(name){
			return '_admin_'+name;
		},
		json_encode:function(data){

			return JSON.stringify(data);
		},
		json_decode:function(json_str){
			try{
				var obj = eval('(' + json_str + ')');
			}
			catch(e){
				console.log(e);
				obj = false;
			}
			return obj;
		}
	};
	
	function lenFor(str){
		
		var  bl = new Blob([str]);
		return bl.size;
		var realLength = 0;
		for (var i = 0; i < str.length; i++) 
		{
			charCode = str.charCodeAt(i);
			if (charCode >= 0 && charCode <= 128) 
			realLength += 2;
			else 
			realLength += 4;
		}
		return realLength;
	}
	
	result.init();
	return result;
})();


Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


Array.prototype.in_array = function(stringToSearch) {
	for (var s = 0; s < this.length; s++) {
		var thisEntry = this[s].toString();
		if (thisEntry == stringToSearch) {
			return true;
		}
	}
	return false;
}


function unique(arr,cparam){
	var cparam = cparam || '';
	
	var result = [];
	var obj = {};
	for(var i=0;i<arr.length;i++){
		
		var val = arr[i];
		
		if(cparam!=''){
			var tt = val[cparam];
			
		}
		else{
			var tt = val;
		}
		
		
		if(!obj[tt]){
			result.push(val);
			obj[tt] = true;
		}
	}
	
	return result
}

export default Wcore;